package matching;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;


public class participant {
	String togive;
	String totake;
	String url;
	int listenPort;
	boolean onlyPost = false;
	
	final String appendURL = "/rides";
	

	
	public participant(String _url, String _togive, String _totake, int _listenPort)
	{
		url = _url + appendURL;
		togive = _togive;
		totake = _totake;
		listenPort = _listenPort;
	}
	
	public participant(String _url, String _togive, String _totake, int _listenPort, boolean _onlyPost)
	{
		url = _url + appendURL;
		togive = _togive;
		totake = _totake;
		listenPort = _listenPort;
		onlyPost = _onlyPost;
	}
	
	public void start()
	{
		final long startTime = System.nanoTime();
		final long endTime;
		try {
		  
		
		this.postToForm();
		if(onlyPost == true)
		{
			return;
		}
		BufferedReader in = null;
		Socket client = null;
		boolean done = false;
		try {
			ServerSocket listener = new ServerSocket(listenPort);
	
			
			
			while(!done)
			{
				System.out.print("Listening for connections on port " + listenPort + "... ");
			    client = listener.accept();
			    in = new BufferedReader(new InputStreamReader(client.getInputStream()));
			    System.out.println("Connected - "+client.getInetAddress());
			    String inputLine;
			    while ((inputLine = in.readLine()) != null) {
			    	System.out.println("From Supervisor: " + inputLine);
			    }
			    done = true;
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally {
			if (in != null)
				try {
					in.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			if (client != null)
				try {
					client.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		}
		
		} finally {
			  endTime = System.nanoTime();
			}
			final long duration = endTime - startTime;
			//convert to seconds 
			double seconds = (double)duration / 1000000000.0;
			System.out.println("It took " + seconds );
		
	}
	
	public void postToForm()
	{
			PostMethod post = new PostMethod(url);
			InetAddress inetAddress = null;
			
			//Hack for linux machines so they don't return 127.0.0.1
			InternetAddress i = new InternetAddress();
			try {
				inetAddress = i.getFirstNonLoopbackAddress(true, false);
			} catch (SocketException e1) {
				System.out.println("ISSUE USING GETNONFIRSTLOOPBACKADDRESS");
				e1.printStackTrace();
			}
			
			String ipAddress = inetAddress.getHostAddress() +":"+listenPort;
			
			NameValuePair[] data = {
					  new NameValuePair("ride[togive]", togive),
					  new NameValuePair("ride[totake]", totake),
					  new NameValuePair("ride[contact]", ipAddress)
					};
			
			post.setRequestBody(data);
			System.out.println("Adding: " + togive + ":" + totake + " to database");
			// execute method and handle any error responses.
			try {
				new HttpClient().executeMethod(post);
			} catch (HttpException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		

	}//posttoForm

}
